﻿Ext.define('app.system.system_role_auth.ProjectAuth', {
    extend: 'Ext.panel.Panel',
    layout: 'border',
    initComponent: function () {
        var me = this;
        me.items = [me.getRoleGrid(), me.getPermissionGrid()];
        me.callParent();
    },
    getRoleGrid: function () {
        var me = this;
        if (!this.roleGrid) {
            this.roleGrid = new Ext.grid.Panel({
                title: localize('RoleList'),
                region: 'west',
                split: true,
                width: 300,
                border: false,
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                forceFit: true,
                store: Ext.create("app.ux.PrmStore", {
                    url: '/api/services/app/role/GetRoles'
                    , remoteSort: false,
                    sorters: [{
                        //先按年龄降序  
                        property: 'name',
                        direction: 'DESC'
                    }],
                        rootProperty: 'result.items'
        }),
        columns: [
            { header: localize('RoleName'), dataIndex: 'name' },
            { header: localize('DisplayName'), dataIndex: 'displayName' },
            {
                header: localize('CreateOn'), dataIndex: 'creationTime', renderer: function (value, p, record) {
                    return Ext.util.Format.date(value, 'Y-m-d');
                }
            }
        ],
        listeners: {
            select: function (grid, record, index, eOpts) {
                me.getPermissionGrid().store.load({ params: { roleId: record.data.id } });
            }
        }
    });
}
return this.roleGrid;
},
getPermissionGrid: function () {
    var me = this;
    if (!this.permissionGrid) {
        var store = Ext.create("app.ux.PrmStore", { url: '/api/services/app/role/GetProjectPermission', autoLoad: false });
        store.load({ params: { roleId: 0 } });
        this.permissionGrid = new Ext.grid.Panel({
            title: localize('PermissionsList'),
            region: 'center',
            border: false,
            viewConfig: {
                enableTextSelection: true //可以复制单元格文字
            },
            forceFit: true,
            tbar: [
                { text: localize('Save'), icon: '/Content/images/page_save.png', handler: function () { me.save(); } },
                "-",
                {
                    text: localize('SelectAll'), icon: '/Content/images/accept.png', handler: function () {
                        for (var i = 0; i < store.getCount() ; i++) {
                            var record = store.getAt(i);
                            record.data.hasAuth = true;
                            record.commit();
                        }
                    }
                },
                "-",
                {
                    text: localize('UnselectAll'), icon: '/Content/images/cancel.png', handler: function () {
                        for (var i = 0; i < store.getCount() ; i++) {
                            var record = store.getAt(i);
                            record.data.hasAuth = false;
                            record.commit();
                        }
                    }
                }
                //"-",
                //localize('VisibleRange') + "：",
                //{
                //    xtype: 'combo',
                //    editable: false,
                //    store: ['Null', 'all', 'business', 'center', 'room'],
                //    listeners: {
                //        select: function (combo, record, eOpts) {
                //            var value = combo.getValue();
                //            for (var i = 0; i < store.getCount() ; i++) {
                //                var record = store.getAt(i);
                //                record.data.scope = value;
                //                record.commit();
                //            }
                //        }
                //    }
                //}
            ],
            plugins: {
                ptype: 'cellediting',
                clicksToEdit: 1
            },
            store: store,
            columns: [
                { header: localize('PermissionEntry'), sorttable: false, dataIndex: 'permission' },
                { xtype: 'checkcolumn', sorttable: false, header: localize('PermissionEntry'), dataIndex: 'hasAuth' }
                //{
                //    header: localize('VisibleRange'), sorttable: false, dataIndex: 'scope', editor: {
                //        xtype: 'combo',
                //        editable: false,
                //        store: ['Null', 'all', 'business', 'center', 'room']
                //    }
                //}
            ]
        });
    }
    return this.permissionGrid;
},
save: function () {
    var me = this;
    var sel = me.getRoleGrid().getSelection();
    if (sel.length == 0) {
        Ext.Msg.alert('系统提示', '请选择授权角色');
        return false;
    }
    var roleId = sel[0].data.id;

    var store = me.getPermissionGrid().store;
    var arry = [];
    for (var i = 0; i < store.getCount() ; i++) {
        var record = store.getAt(i);
        arry.push({
            Permission: record.data.permission,
            HasAuth: record.data.hasAuth
            //Scope: record.data.scope
        });
    }


    App.AjaxPost('/api/services/app/role/SetProjectPermission', { RoleId: roleId, Permissions: arry }, function (data) {
        me.getPermissionGrid().store.load({ params: { roleId: roleId } });
    });


}
});